<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>14、createRefAPI存储ref</title>
</head>
<body>
    <div id="test"></div>

    <script type="text/javascript" src="../js/react.development.js"></script>
    <script type="text/javascript" src="../js/react-dom.development.js"></script>
    <script type="text/javascript" src="../js/babel.min.js"></script>
    <!-- 对props属性类型，以及是否必传进行限制 -->
    <script type="text/javascript" src="../js/prop-types.js"></script>
    
    <script type="text/babel"> 
        class Demo extends React.Component{
            myRef = React.createRef()
            // 专人专用，每次需要存储一个ref，都需要执行一次createRef，创建一个容器
            showData = () =>{
                console.log(this.myRef)
                alert(this.myRef.current.value);
            }
            showData2 = () =>{
                const {input2} = this
                alert(input2.value)
            }
            render(){
                // ref即结点标识，相当于id，能够获取到真实的dom
                return(
                    // 字符串形式的ref基本废除，用的比较少，主要原因是效率
                    <div>
                        <input ref={this.myRef} type="text" placeholder="点击按钮提示数据" />
                        <button onClick={this.showData}>点我提示左侧的数据</button>
                        <input ref={c => this.input2 = c} type="text" onBlur={this.showData2} placeholder="失去焦点提示数据" />
                    </div>
                )
            }
        }

        ReactDOM.render(<Demo />, document.getElementById('test'))
    </script>
</body>
</html>